package com.qianli.cashmama.product.server.domain;

import com.alibaba.fastjson.JSON;
import com.qianli.cashmama.product.facade.enums.SellStatusEnum;
import com.qianli.cashmama.product.facade.result.Product;
import com.qianli.cashmama.product.facade.result.ProductConfig;
import com.qianli.cashmama.product.facade.result.ProductRuleDetail;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by xuyang on 2017/7/18.
 */

public class ProductDomain {

    //产品编码
    private String code;

    //本金
    private BigDecimal principalMoney;

    //实际到账金额
    private BigDecimal arrivalMoney;

    //还款周期天数
    private Integer paybackPeriodLength;

    //还款周期数，比如，分3期还钱，则totalPayBackPeriodCount = 3
    private Integer totalPaybackPeriodCount;

    //服务费（前置服务费 + 税费）
    private BigDecimal serviceCharge;

    //前置服务费
    private BigDecimal preServiceCharge;

    //税费
    private BigDecimal gstCharge;

    //一次性登机费
    private BigDecimal oneTimeOnBoardingFee;

    //逾期服务费
    private BigDecimal overdueServiceCharge;

    //利息费率
    private BigDecimal rate;

    //罚息费率
    private BigDecimal penaltyRate;

    //服务费率
    private BigDecimal serviceFeeRate;

    //产品规则
    private List<BusinessRuleDetailDomain> productRules;

    //产品所属类目
    private ProductCategoryDomain productCategoryDomain;

    /**
     * 货架类型 针对不同类目可以有不同的含义 1-默认
     */
    private Integer shelfType;

    //产品协议，一个url
    private String protocol;

    //产品logo url
    private String logo;

    //是否上架
    /**
     * @see SellStatusEnum
     */
    private SellStatusEnum isSelling;

    //产品有效期开始时间
    private Date startTime;

    //产品有效期结束时间
    private Date endTime;

    private String showFlag;
    /**
     * 产品风险等级
     */
    private Integer riskLevel;
    /**
     * 产品配置 json格式
     */
    private String config;

    public Product newFacade(boolean needRules) {
        Product product = new Product();
        product.setCode(code);
        product.setPrincipalMoney(principalMoney.toString());
        product.setArrivalMoney(arrivalMoney.toString());
        product.setPaybackPeriodLength(paybackPeriodLength);
        product.setTotalPaybackPeriodCount(totalPaybackPeriodCount);
        product.setServiceCharge(serviceCharge.toString());
        product.setPreServiceCharge(preServiceCharge.toString());
        product.setGstCharge(gstCharge.toString());
        product.setOneTimeOnBoardingFee(oneTimeOnBoardingFee.toString());
        product.setOverdueServiceCharge(overdueServiceCharge.toString());
        product.setRate(rate.toString());
        product.setPenaltyRate(penaltyRate.toString());
        product.setServiceFeeRate(serviceFeeRate.toString());
        product.setProductCategory(productCategoryDomain.newFacade(needRules));
        product.setShelfType(shelfType);
        product.setProtocol(protocol);
        product.setLogo(logo);
        product.setShowFlag(showFlag);
        product.setRiskLevel(riskLevel);
        Date now = new Date();
        if (isSelling.getStatus() == SellStatusEnum.ON_SELL.getStatus()
                && startTime.before(now)
                && endTime.after(now)
                && productCategoryDomain.getIsSelling().getStatus() == SellStatusEnum.ON_SELL.getStatus()) {
            product.setActive(true);
        } else {
            product.setActive(false);
        }
        if (needRules && productRules != null) {
            List<ProductRuleDetail> facadeRules = new ArrayList<>();
            for (BusinessRuleDetailDomain ruleDetailDomain : productRules) {
                facadeRules.add(ruleDetailDomain.newFacade());
            }
            product.setProductRules(facadeRules);
        }
        product.setConfig(StringUtils.isEmpty(config) ? null : JSON.parseObject(config, ProductConfig.class));
        return product;
    }

    public String getShowFlag() {
        return showFlag;
    }

    public ProductDomain setShowFlag(String showFlag) {
        this.showFlag = showFlag;
        return this;
    }

    public String getCode() {
        return code;
    }

    public ProductDomain setCode(String code) {
        this.code = code;
        return this;
    }

    public BigDecimal getPrincipalMoney() {
        return principalMoney;
    }

    public ProductDomain setPrincipalMoney(BigDecimal principalMoney) {
        this.principalMoney = principalMoney;
        return this;
    }

    public BigDecimal getArrivalMoney() {
        return arrivalMoney;
    }

    public ProductDomain setArrivalMoney(BigDecimal arrivalMoney) {
        this.arrivalMoney = arrivalMoney;
        return this;
    }

    public Integer getPaybackPeriodLength() {
        return paybackPeriodLength;
    }

    public ProductDomain setPaybackPeriodLength(Integer paybackPeriodLength) {
        this.paybackPeriodLength = paybackPeriodLength;
        return this;
    }

    public Integer getTotalPaybackPeriodCount() {
        return totalPaybackPeriodCount;
    }

    public ProductDomain setTotalPaybackPeriodCount(Integer totalPaybackPeriodCount) {
        this.totalPaybackPeriodCount = totalPaybackPeriodCount;
        return this;
    }

    public BigDecimal getServiceCharge() {
        return serviceCharge;
    }

    public ProductDomain setServiceCharge(BigDecimal serviceCharge) {
        this.serviceCharge = serviceCharge;
        return this;
    }

    public BigDecimal getOverdueServiceCharge() {
        return overdueServiceCharge;
    }

    public ProductDomain setOverdueServiceCharge(BigDecimal overdueServiceCharge) {
        this.overdueServiceCharge = overdueServiceCharge;
        return this;
    }

    public BigDecimal getRate() {
        return rate;
    }

    public ProductDomain setRate(BigDecimal rate) {
        this.rate = rate;
        return this;
    }

    public BigDecimal getPenaltyRate() {
        return penaltyRate;
    }

    public ProductDomain setPenaltyRate(BigDecimal penaltyRate) {
        this.penaltyRate = penaltyRate;
        return this;
    }

    public List<BusinessRuleDetailDomain> getProductRules() {
        return productRules;
    }

    public ProductDomain setProductRules(List<BusinessRuleDetailDomain> productRules) {
        this.productRules = productRules;
        return this;
    }

    public ProductCategoryDomain getProductCategoryDomain() {
        return productCategoryDomain;
    }

    public ProductDomain setProductCategoryDomain(ProductCategoryDomain productCategoryDomain) {
        this.productCategoryDomain = productCategoryDomain;
        return this;
    }

    public Integer getShelfType() {
        return shelfType;
    }

    public ProductDomain setShelfType(Integer shelfType) {
        this.shelfType = shelfType;
        return this;
    }

    public String getProtocol() {
        return protocol;
    }

    public ProductDomain setProtocol(String protocol) {
        this.protocol = protocol;
        return this;
    }

    public String getLogo() {
        return logo;
    }

    public ProductDomain setLogo(String logo) {
        this.logo = logo;
        return this;
    }

    public SellStatusEnum getIsSelling() {
        return isSelling;
    }

    public ProductDomain setIsSelling(SellStatusEnum isSelling) {
        this.isSelling = isSelling;
        return this;
    }

    public Date getStartTime() {
        return startTime;
    }

    public ProductDomain setStartTime(Date startTime) {
        this.startTime = startTime;
        return this;
    }

    public Date getEndTime() {
        return endTime;
    }

    public ProductDomain setEndTime(Date endTime) {
        this.endTime = endTime;
        return this;
    }

    public BigDecimal getServiceFeeRate() {
        return serviceFeeRate;
    }

    public ProductDomain setServiceFeeRate(BigDecimal serviceFeeRate) {
        this.serviceFeeRate = serviceFeeRate;
        return this;
    }

    public Integer getRiskLevel() {
        return riskLevel;
    }

    public ProductDomain setRiskLevel(Integer riskLevel) {
        this.riskLevel = riskLevel;
        return this;
    }

    public BigDecimal getPreServiceCharge() {
        return preServiceCharge;
    }

    public ProductDomain setPreServiceCharge(BigDecimal preServiceCharge) {
        this.preServiceCharge = preServiceCharge;
        return this;
    }

    public BigDecimal getGstCharge() {
        return gstCharge;
    }

    public ProductDomain setGstCharge(BigDecimal gstCharge) {
        this.gstCharge = gstCharge;
        return this;
    }

    public String getConfig() {
        return config;
    }

    public ProductDomain setConfig(String config) {
        this.config = config;
        return this;
    }

    public BigDecimal getOneTimeOnBoardingFee() {
        return oneTimeOnBoardingFee;
    }

    public void setOneTimeOnBoardingFee(BigDecimal oneTimeOnBoardingFee) {
        this.oneTimeOnBoardingFee = oneTimeOnBoardingFee;
    }
}
